set more off

use dataset, clear

gen B = .
gen UB = .
gen LB = .

* programs to store estimates (eststo doesn't work well with cgmboot)
capture program drop myest1
program define myest1
	args i bw cov
	mat bootresults = e(bootresults)
	svmat bootresults
	replace B = _b[Zz] if index == `i'
	_pctile bootresults6 if bootresults3 == 0, nq(1000)
	replace LB = `r(r25)' if index == `i'
	replace UB = `r(r975)' if index == `i'
	drop bootresults*
end
 
local Z pcteligible2013

su `Z', det
gen Z = `Z' >=r(p50)
replace Z = . if mi(`Z')
 
gen Zx = Z*x
gen Zz = Z*z
gen Zzx = Z*zx

keep if abs(x) < 100

local nreps 500

foreach y in pctui {

	if "`y'" == "registration" local mytitle = "Registration"
	if "`y'" == "turnout" local mytitle = "Turnout"

	preserve

		gen index = _n + 2007
		 
		cgmwildboot d`y'20092008 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2009
		cgmwildboot d`y'20102009 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2010
		cgmwildboot d`y'20112010 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2011
		cgmwildboot d`y'20122011 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2012
		cgmwildboot d`y'20132012 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2013
		cgmwildboot d`y'20142013 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2014
		cgmwildboot d`y'20152014 z Z Zz x zx Zx Zzx `covariatesp' , cl(fipsstate) bootcluster(fipsstate) reps(`nreps')
		myest1 2015
		
		keep if !mi(B)

		#delimit;

		gr tw
			(sc B index, col(black))
			(rspike UB LB index, col(black))
			,
				xlab(2008(2)2016)
				xline(2013.5)
				ylab(, angle(horiz))
				yline(0)
				legend(off)
				plotregion(style(none))
				name(g`y', replace)
				title(`mytitle')
				ytitle("Estimate")
				xtitle("Year")
				;
				
		#delimit cr

	restore

}

gr combine gpctui

gr export "_output/figureA12.pdf", replace
gr close
